package com.example.mall.dao;

import com.example.mall.model.Subscribe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class SubscribeDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void insert(Subscribe subscribe) {
        String sql = "insert into subscribe (bID, sID) values (?, ?)";
        jdbcTemplate.update(sql, subscribe.getbID(), subscribe.getsID());
    }

    public void delete(Integer bID, Integer sID) {
        String sql = "delete from subscribe where bID = ? and sID = ?";
        jdbcTemplate.update(sql, bID, sID);
    }

    public List<Subscribe> getByBid(String bID) {
        String sql = "select * from account where bID = ? lock in share mode";
        RowMapper<Subscribe> rowMapper = new BeanPropertyRowMapper<>(Subscribe.class);
        return jdbcTemplate.query(sql, rowMapper, bID);
    }

}
